import Vue from 'vue'
import { limitMoney, isAndroid } from './common'
import { formatMoney } from '@/assets/js/filters'

Vue.directive('limit-money', (el, binding) => {
  if (isAndroid) {
    el.setAttribute('type', 'tel')
  }
  el.addEventListener('keyup', () => {
    el.value = limitMoney(el.value)
  }, false)
  el.addEventListener('change', () => {
    el.value = limitMoney(el.value)
  }, false)
  el.addEventListener('blur', () => {
    let max = binding.value ? (binding.value.max || 0) : 0
    if (max && Number(el.value) > max) {
      el.value = formatMoney(max) + ''
    } else {
      el.value = formatMoney(el.value) + ''
    }
  }, false)
})

Vue.directive('auto-scroll', (el, binding) => {
  if (isAndroid) {
    el.addEventListener('click', () => {
      setTimeout(() => {
        el.scrollIntoView(false)
      }, 422)
    }, false)
  }
})
